package Controllers;

import DB.DBCustomer;
import DB.DBLocation;
import Models.Customer;
import java.util.ArrayList;
import java.util.HashMap;

// <editor-fold defaultstate="collapsed" desc=" UML Marker ">
// #[regen=yes,id=DCE.E4035498-FE8A-18CF-AB70-11AA215B2752]
// </editor-fold> 
public class CustomerController extends ControllerAbstract<Customer>
{
	// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
	// #[regen=yes,id=DCE.B1B3ADC1-548B-2121-3B72-D61634646A49]
	// </editor-fold> 
	public CustomerController ()
	{}

	public int insert(HashMap data)
	{
		HashMap loData = new HashMap();
		HashMap cuData = new HashMap();

		loData.put("address", data.get("address"));
		loData.put("zipCode", data.get("zipCode"));

		cuData.put("name", data.get("name"));
		cuData.put("cvr", data.get("cvr"));
		cuData.put("phoneNo", data.get("phoneNo"));

		DBLocation dbl = new DBLocation();
		int locationNo = dbl.insert(loData);
		//dbl.fetchLast().getLocationNo()
		cuData.put("locationNo", locationNo);

		DBCustomer dbc = new DBCustomer();
		return dbc.insert(cuData);
	}

	public Customer find(String id)
	{
		DBCustomer dbc = new DBCustomer();
		return dbc.fetchRow("customerNo = "+id, null);
	}

	public int update(HashMap data, String where, int limit)
	{
		HashMap loData = new HashMap();
		HashMap cuData = new HashMap();

		loData.put("address", data.get("address"));
		loData.put("zipCode", data.get("zipCode"));

		DBLocation dbl = new DBLocation();
		dbl.update(loData, "locationNo = "+data.get("locationNo"), 1);

		cuData.put("name", data.get("name"));
		cuData.put("cvr", data.get("cvr"));
		cuData.put("phoneNo", data.get("phoneNo"));

		DBCustomer dbc = new DBCustomer();
		return dbc.update(data, where, limit);
	}

	public int delete(int id)
	{
		DBCustomer dbc = new DBCustomer();
		return dbc.delete("customerNo = "+id, 1);
	}

	public ArrayList<Customer> fetchAll()
	{
		DBCustomer dbc = new DBCustomer();
		return dbc.fetchAll();
	}

	public ArrayList<Customer> searchByName(String searchText, String order, int limit)
	{
		DBCustomer dbc = new DBCustomer();
		return dbc.fetchAll("name = '"+searchText+"'", order, limit);
	}
}